import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
    selector: 'sa-coefficient-log',
    templateUrl: './coefficient-log.component.html'
})
export class CoefficientLogComponent implements OnInit {
    tableData: any = []; // table的表单数据
    totalCounts: number = 0; //分页总条数
    curPage: number = 1; //分页当前页
    pageSize: number = 10; //内置的分页每页条数
    activeSelected: string = ''; //row高亮

    // 查询参数
    tableSearchParam: any = {
        companyId: '',  //公司id
        companyName: '',  //公司名称
        deviceId:'',
        carNumber:'',
        type: '',
        startTime: '',  //开始时间
        endTime: '',   //结束时间
        isPush: '' , //是否推送
        useTypeL2: '', //过滤条件:车辆分类二级
        useTypeL1: '' //过滤条件:车辆分类一级
    }
    constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
        activatedRoute.queryParams.subscribe(queryParams => {
        });
    }
    ngOnInit() {
        // this.load();
    }
    //公司[项目]选中回调
    companySelected(data) {
        this.tableSearchParam.companyId = data.companyId;
        this.tableSearchParam.companyName = data.companyName;
        this.tableSearchParam.deviceId = data.deviceId;
        this.tableSearchParam.carNumber = data.carNumber;
        this.tableSearchParam.useTypeL2 = data.useTypeL2;
		this.tableSearchParam.useTypeL1 = data.useTypeL1;
    }

    // 时间选择回调
    dateSelect(time) {
        this.tableSearchParam.startTime = time.startTime;
        this.tableSearchParam.endTime = time.endTime;
        this.tableSearch()
    }

    // 获取列表数据
    load() {
        let param = {
            "pageSize": this.pageSize,
            "nowPage": this.curPage,
            "parameters": {
                companyId: this.tableSearchParam.companyId,
                type: this.tableSearchParam.type,
                carNumber: this.tableSearchParam.carNumber,
                isPush: this.tableSearchParam.isPush,
                deviceId: this.tableSearchParam.deviceId,
                useTypeL2: this.tableSearchParam.useTypeL2,
                startTime: this.tableSearchParam.startTime,
                endTime: this.tableSearchParam.endTime,
                export: '',
            }
        }
        let reqdata = 'gridPager=' + JSON.stringify(param);
        this.jsonApiService.http("deviceCoefLog/pushCoefLogList.html", reqdata).subscribe(result => {
            if (result.isSuccess && result.exhibitDatas) {
                this.tableData = result.exhibitDatas
                this.totalCounts = result.recordCount;

                for (let item of result.exhibitDatas) {
                    if (item.ceof) {
                        let ceof = [];
                        for (let i = 0; i < item.ceof.length; i++) {
                            let _item = item.ceof[i];
                            if (_item) {
                                let obj: any = {
                                    value: item.ceof[i],
                                    index: i + 1
                                }
                                ceof.push(obj);
                            }
                        }
                        item.ceof = ceof;
                    } else {
                        item.ceof = [];
                    }
                }
            }
        });
    }

    // 分页回调
    paginate(e) {
        this.activeSelected = '';
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
    // 刷新表
    refresh() {
        this.load();
    }
    // 表单查询
    tableSearch() {
        this.curPage = 1;
        this.load();
    }

    // 数据导出
    exportData() {
        let param = {
            "pageSize": this.pageSize,
            "nowPage": this.curPage,
            "parameters": {
                companyId: this.tableSearchParam.companyId,
                type: this.tableSearchParam.type,
                carNumber: this.tableSearchParam.carNumber,
                isPush: this.tableSearchParam.isPush,
                deviceId: this.tableSearchParam.deviceId,
                useTypeL2: this.tableSearchParam.useTypeL2,
                startTime: this.tableSearchParam.startTime,
                endTime: this.tableSearchParam.endTime,
                export: '1',
            }
        }
        let timeCheck = common.correctTime(param.parameters, ['export'])
        if (!timeCheck.status) {
            this.jsonApiService.message(timeCheck.message);
            return;
        }
        common.exprotDataFile(param, 'gridPager', config.BaseURL + 'deviceCoefLog/pushCoefLogList.html')
    }
}